package bit2;

import java.util.HashMap;

class HashBuck {
    static class Node{
        public int key;
        public int val;
        public Node next;
        public Node(int key,int val){
            this.key=key;
            this.val=val;
        }
    }
    public Node[] array;
    public int usedSize;
    public HashBuck() {
        this.array = new Node[8];
    }
    public int get(int key){
        int index=key% array.length;
        Node cur=array[index];
        while (cur!=null){
            if (cur.key==key){
                return cur.val;
            }
            cur=cur.next;
        }
        return -1;
    }
    public void put(int key,int val){
        int index=key% array.length;
        Node cur=array[index];
        Node node1=null;
        while (cur!=null){
            if (cur.key==key){
                cur.val=val;
                return;
            }
            node1=cur;
            cur=cur.next;
        }
        Node node=new Node(key, val);
        if (array[index]==null){
            array[index]=node;
        }
        else {
            node1.next=node;
        }
        this.usedSize++;
        if (loadFactor()>=0.75){
            resize();
        }
    }
    public double loadFactor(){
        return this.usedSize*1.0/array.length;
    }
    public void resize(){
        Node[] newarray=new Node[array.length*2];
        for (int i = 0; i < array.length; i++) {
            Node cur=array[i];
            while (cur!=null){
                Node curNext=cur.next;
                int index= cur.key% newarray.length;
                Node curN=newarray[index];
                if (curN==null){
                    newarray[index]=cur;
                }
                else {
                    while (curN.next!=null){
                        curN=curN.next;
                    }
                    curN.next=cur;
                }
                cur.next=null;
                cur=curNext;
            }

        }
        array=newarray;
    }
}
public class bitMain11 {
    public static void main(String[] args) {
        HashMap<String,String> map=new HashMap<>(16);
        map.put("da","ad");
        HashBuck hashBuck = new HashBuck();
        hashBuck.put(1,11);
        hashBuck.put(2,22);
        hashBuck.put(3,33);
        hashBuck.put(17,177);//1
        hashBuck.put(24,244);//0
        hashBuck.put(15,155);//7
        System.out.println("oooo");
    }
}
